package com.qingke.javatest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.qingke.javatest.mybatis.pojo.MyClass;
import com.qingke.javatest.mybatis.pojo.Student;
import com.qingke.javatest.mybatis.pojo.User;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {

		String resource = "mybatis_conf.xml";
		InputStream is=null;
		is=App.class.getClassLoader().getResourceAsStream(resource);
//		try {
//			is = new FileInputStream(new File("src/main/resources/mybatis_conf.xml"));
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
//		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is, "test2");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);

		SqlSession session = sessionFactory.openSession();

//		users(session);
//		System.out.println("======================================");
//		userById(session);
//		System.out.println("======================================");
//		userHashMap(session);
//		System.out.println("======================================");
//		userExample(session);
//		System.out.println("======================================");
//		User user1=new User();
//		user1.setCode("zjd");
//		user1.setName("zjd");
//		user1.setPassword("666");
//		insertUser(session,user1);
//		
//		user1.setPassword("888");
//		updateUser(session,user1);
//		deleteUser(session,user1);

//		listMyClasses(session);
		
//		
//		System.out.println("======================================");
//		myClassById(session);
//		System.out.println("======================================");
//		
		listStudents(session);
//		System.out.println("======================================");
//		getStudentsNameLike(session);
//		System.out.println("======================================");
//		getStudentByConditionsOr(session);
//		System.out.println("======================================");
//		getStudentByConditionsAnd(session);
//		System.out.println("======================================");
//		updateStudentById(session);
//		System.out.println("======================================");
//		selectStudentsIn(session);
		System.out.println("======================================");
		callSP(session);

		session.commit();
		session.close();
	}

	public static void users(SqlSession session) {
		String statement = "com.qingke.javatest.mybatis.mapping.userMapper.getAllUsers";
		List<User> list = session.selectList(statement);
		for (User u : list) {
			System.out.println(u);
		}
	}

	public static void userById(SqlSession session) {
		String statement = "com.qingke.javatest.mybatis.mapping.userMapper.getUserById";
		User user = session.selectOne(statement, 1);
		System.out.println(user);
	}

	public static void userHashMap(SqlSession session) {
		String statement = "com.qingke.javatest.mybatis.mapping.userMapper.getUserAttrMapById";
		Map<String, Object> map = session.selectOne(statement, 1);
		for (String key : map.keySet()) {
			System.out.println(
					"key=" + key + ",value=" + map.get(key) + ",type=" + map.get(key).getClass().getSimpleName());
		}
	}

	public static void userExample(SqlSession session) {
		//字段不对应，所以id为null
		String statement = "com.qingke.javatest.mybatis.mapping.userMapper.getAllObjects";
		List<User> list = session.selectList(statement);
		for (User u : list) {
			System.out.println(u);
		}
	}

	public static void insertUser(SqlSession session, User user) {
		String statement = "com.qingke.javatest.mybatis.mapping.userMapper.insertUser";
		System.out.println("before=" + user);
		int count = session.insert(statement, user);
		System.out.println("count=" + count);
		System.out.println("after=" + user);
	}

	public static void updateUser(SqlSession session, User user) {
		String statement = "com.qingke.javatest.mybatis.mapping.userMapper.updateUser";
		int count = session.update(statement, user);
		System.out.println("update count=" + count);
		System.out.println("after update=" + user);
	}

	public static void deleteUser(SqlSession session, User user) {
		String statement = "com.qingke.javatest.mybatis.mapping.userMapper.deleteUser";
		int count = session.delete(statement, user);
		System.out.println("delete count=" + count);
		System.out.println("after delete=" + user);
	}
	
	public static void listMyClasses(SqlSession session){
		String statement = "com.qingke.javatest.mybatis.mapping.classMapper.selectClasses";
		List<MyClass> list = session.selectList(statement);
		for (MyClass c : list) {
			System.out.println(c);
		}
	}
	
	public static void myClassById(SqlSession session){
		String statement = "com.qingke.javatest.mybatis.mapping.classMapper.selectMyClassById";
		MyClass c = session.selectOne(statement,1);
		System.out.println(c);
		System.out.println(c.getStudents());
	}
	
	public static void listStudents(SqlSession session){
		String statement = "com.qingke.javatest.mybatis.mapping.studentMapper.selectStudents";
		List<Student> list = session.selectList(statement);
		for (Student s : list) {
			System.out.println(s+""+s.getMyClass());
		}
	}
	
	public static void getStudentsNameLike(SqlSession session){
		String statement = "com.qingke.javatest.mybatis.mapping.studentMapper.getStudentsNameLike";
		Student student=new Student();
		student.setName("%2%");
		List<Student> list = session.selectList(statement,student);
		for (Student s : list) {
			System.out.println(s);
		}
	}
	
	public static void getStudentByConditionsOr(SqlSession session){
		String statement = "com.qingke.javatest.mybatis.mapping.studentMapper.getStudentsByConditionsOr";
		Student student=new Student();
//		student.setName("st2");
		student.setClassId(2);
		List<Student> list = session.selectList(statement,student);
		System.out.println(list.size());
		for (Student s : list) {
			System.out.println(s);
		}
	}
	
	public static void getStudentByConditionsAnd(SqlSession session){
		String statement = "com.qingke.javatest.mybatis.mapping.studentMapper.getStudentsByConditionsAnd";
		Student student=new Student();
		student.setName("st2");
		student.setClassId(1);
		List<Student> list = session.selectList(statement,student);
		System.out.println(list.size());
		for (Student s : list) {
			System.out.println(s);
		}
	}
	
	public static void updateStudentById(SqlSession session){
		String statement = "com.qingke.javatest.mybatis.mapping.studentMapper.updateStudentsById";
		Student student=new Student();
		student.setId(2);
		student.setName("st2");
		student.setClassId(2);
		int count= session.update(statement,student);
		System.out.println("update count:"+count);
	}
	
	public static void selectStudentsIn(SqlSession session){
		String statement = "com.qingke.javatest.mybatis.mapping.studentMapper.selectStudentsIn";
		List<Integer> listInt=new ArrayList<Integer>();
		listInt.add(1);
		listInt.add(2);
		List<Student> list = session.selectList(statement,listInt);
		for (Student s : list) {
			System.out.println(s);
		}
	}
	
	private static void callSP(SqlSession session){
		String st="com.qingke.javatest.mybatis.mapping.studentMapper.callSPtest";
		Map<String,Integer> map = new HashMap<String, Integer>();
		map.put("addend1", 1);
		map.put("addend2", 6);
		session.update(st,map);
		System.out.println(map.get("theSum"));
	}
}
